<?php

// Constants
define("MYSQL_SERVER", "localhost");
define("MYSQL_USER", "rebesche");
define("MYSQL_PWD", "rebesche");
define("MYSQL_DBNAME", "geopuebla");
define("GMAP_KEY", "ABQIAAAAEM61ahr2H7oFi4USwV9w1RRk2MLON6BRSUFSmw_nKHqiy8NUhxQCgCf7a82-VYLNonUwNJk9ugASYA");// local
//define("GMAP_KEY", "ABQIAAAAEM61ahr2H7oFi4USwV9w1RRSuRzUpaag64xslw_1ovSV_gjA9hRUkesrtK6I4bupEVCmiNOXoEAVEg"); //http://liris-web.insa-lyon.fr/geopuebla/


// Centralized DB connection
function DBConnect()
{
	$conn=mysql_connect( MYSQL_SERVER , MYSQL_USER, MYSQL_PWD)or die("Connection to the database not possible");
	mysql_select_db( MYSQL_DBNAME, $conn) or die($dbuser." No se encontró la Base de datos. ".$dbname);
	return $conn;
}

// Centralized DB close
function DBClose($connection)
{
	mysql_close($connection);
}

?>
<?php

// Centralized access to the google map
function GetMap()
{?>
	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo GMAP_KEY ?>"
	type="text/javascript"></script>
<?php
}

function ValideAdmin( $user, $psw ) 
{
	$connection = DBConnect();
	
	$query = sprintf("SELECT * FROM admin WHERE IDadmin='%s' AND password='%s'",
    	mysql_real_escape_string($user),
    	mysql_real_escape_string($psw));
	
	$result = mysql_query($query) or die ('\n error en query ' . mysql_error());

	if (!$result) 
	{
	    $message  = 'Requête invalide : ' . mysql_error() . "\n";
	    $message .= 'Requête complète : ' . $query;
	    die($message);
	}


	while($row = mysql_fetch_assoc($result))
	{
		$db_username=$row['IDadmin'];
		$db_password=$row['password'];
		
		if($db_username="$user" && $db_password="$psw")
		{
			return true;
		}
	}
	
	DBCLose($connection);
	
	return false;
}


function MapInitialization()
{
	echo "map = new GMap2(document.getElementById(\"map_canvas\"));";
	echo "map.addControl(new GSmallMapControl());";
	echo "map.addControl(new GMapTypeControl());";
	echo "map.setCenter(new GLatLng(19.043308,-98.19726), 16);";
	echo "map.setMapType(G_HYBRID_MAP);";
	echo "map.enableScrollWheelZoom();"; 
	
}

function DisplayMarkersOnMap()
{
	//Conexion a mysql y a la base de datos
	$connection = DBConnect();

	//Selecciona todos los lugares que estan guardados
	$query="select * FROM places"; 
	$resultado = mysql_query($query) or die ('\n error en query $query' . mysql_error());
	while($row = mysql_fetch_assoc($resultado))
	{
		$id=$row['IDplaces'];
		$x=$row['CoordX'];
		$y=$row['CoordY'];
		$name=$row['Name'];
		$adress=$row['Adress'];
		$mi=$row['MoreInfo'];
		$image=$row['Image'];

		//Codigo html para lo que sera desplegado en 
		$html="<h1>$name</h1>".
		"<img src=img/$image height=300 weight=300 align=center>".
		"<p><b>Adress:</b> $adress</p>".
		"<p>$mi</p>".
		"<table width=90% align=center>".
		"<tr><td>".
		"<form name=tour action=Tours.php method=post target=leftFrame>".
		"<input type=hidden name=id value=$id>".
		"<input type=submit name=submit value=CreateTour onclick=return zoom(19.043341, -98.198397);>".
		"</form>".
		"</td>".
		"<td align=right>".
		"</td></tr>".
		"</table>";	
		//Llena el mapa con los marcadores correspondiendes 
		echo "marker = new PdMarker(new GLatLng($x, $y));";
		echo "marker.setTooltip('$name');";
		echo "marker.setHoverImage('http://www.google.com/mapfiles/dd-start.png');";
		echo "marker.setDetailWinHTML('$html');";
		echo "map.addOverlay(marker);";		
	}

	DBCLose($connection);
}




function DisplayTourOnTheMap($idp)
{
	//Conexion a mysql y a la base de datos
	$connection = DBConnect();
	
	//Selecciona todos los lugares de la base de datos
	$query2="select * from places"; 
	$resultado2 = mysql_query($query2) or die ('\n error en query ' . mysql_error());
	$xant=NULL;
	$yant=NULL;
	$num=0;
	while($row = mysql_fetch_assoc($resultado2)){
		$id=$row['IDplaces'];
		$check= $_POST["$id"];
		//selecciona cuales pertenecen al tour
		if ($check!="" || $id==$idp){ 
			$num=$num+1;
			$x=$row['CoordX'];
			$y=$row['CoordY'];
			$name=$row['Name'];
			$adress=$row['Adress'];
			$mi=$row['MoreInfo'];
			$image=$row['Image'];

			if($num==1){
				$x1=$x;
				$y1=$y;
			}
			
			//Codigo html para lo que sera desplegado en  el marcador
			$html="<h1>$name</h1>".
			"<img src=img/$image height=300 weight=300 align=center>".
			"<p><b>Adress:</b> $adress</p>".
			"<p>$mi</p>";	
			//Llena el mapa con los marcadores correspondiendes 
			echo "marker = new PdMarker(new GLatLng($x, $y));";
			echo "marker.setTooltip('$name');";
			echo "marker.setHoverImage('http://www.google.com/mapfiles/dd-start.png');";
			echo "marker.setDetailWinHTML('$html');";
			echo "map.addOverlay(marker);";	
			//Crea las lineas que unen esos puntos
			if($xant!=NULL && $yant!=NULL){	
				echo "var polyline = new GPolyline([new GLatLng($xant, $yant),new GLatLng($x, $y)], '#0099CC', 5, 0.7);";
				echo "map.addOverlay(polyline);";
			}
			
			$xant=$x;
			$yant=$y;		
		}
	}

	echo "var polyline = new GPolyline([new GLatLng($x, $y),new GLatLng($x1, $y1)], '#0099CC', 5, 0.7);";
	echo "map.addOverlay(polyline);";

	DBCLose($connection);

}


// Get hash table of all event in the return value
// $tab[ idEvent ] => stringEvent
function GetHashTableEvents()
{
	$connection = DBConnect();
        
    $query="select e.IDevents idEvent, e.Name eventName, p.Name placeName, e.Date eventDate FROM events e, places p where e.Place = p.IDplaces"; 
    $resultado = mysql_query($query) or die ('\n error en query ' . mysql_error());
	$num_rows = mysql_num_rows($resultado);
	
	$tab = array();
	if( !($num_rows == '0') ){
		while($row = mysql_fetch_assoc($resultado)){
			$id=$row['idEvent'];
			$name= $row["eventName"];
			$addressName= $row["placeName"];	
			$eventDate= $row["eventDate"];
			
			$tab[ $id ] = "$name - $addressName - $eventDate";
		}
	}
	DBCLose($connection);
	
	return $tab;
}

?>

